#include "TournamentEnv.hpp"

#include <algorithm>
#include <cstdio>
#include <ctime>

#include <iostream>

void TournamentEnv::setGroupSize(const unsigned &size)
{
  if(size>0)
    {
      _groupSize=size;
    }
}

void TournamentEnv::setIfReturning(const bool &returning)
{
  _returning=returning;
}

void TournamentEnv::selection()
{
  srand(time(NULL));
  std::vector<Specimen> tournamentGroup;

  _parents.clear();
  while(_parents.size()<_maxPopulation)
    {
      tournamentGroup.clear();
      for(unsigned i=0;i<_groupSize;++i)
	{
	  int chosen=rand()%_children.size();
	  tournamentGroup.push_back(_children[chosen]);
	  _children.erase(_children.begin()+chosen);
	}
      std::sort(tournamentGroup.begin(),tournamentGroup.end());
      _parents.push_back(tournamentGroup.front());//sort sorts ascending
      if(!_returning)
	{
	  tournamentGroup.erase(tournamentGroup.begin());
	}
      while(!tournamentGroup.empty())
	{
	  _children.push_back(tournamentGroup.back());
	  tournamentGroup.pop_back();
	}
    }
}
